/**
 * 
 */
package cn.ivicar.app.controller.service;

import java.util.List;

import com.google.inject.Inject;

import cn.com.carsmart.exception.CarsmartException;
import cn.com.carsmart.ws.ibatis.model.BaseExample;
import cn.com.carsmart.ws.ibatis.model.BaseExample.Criteria;
import cn.com.carsmart.ws.model.Vehicle;
import cn.com.carsmart.ws.service.VehicleManager;

/**
 * 管理车辆信息
 * @author liurunbin 2017年5月18日 cn.ivicar.app.controller.service
 */
public class VehicleService {

	@Inject
	private VehicleManager vehicleManager;
    /*
    按VehicleId获取车辆信息
     */
	public Vehicle get(Long vehicleId) {
		return vehicleManager.get(vehicleId);

	}

	/*
	 根据vehicleId查除了当前的记录(licensePlate)

	  @return
	 */
	public boolean validateVehicleByLicensePlate(Long vehicleId, String licensePlate) {
		BaseExample example = vehicleManager.getExample();
		Criteria c = example.or();
		if (vehicleId != null) {
			c.andPropertyNotEqualTo(vehicleId, "id");
		}
		c.andPropertyEqualTo(licensePlate, "licensePlate");
		int count = vehicleManager.countByExample(example);
		return count > 0 ? true : false;
	}

	/*
	  根据vehicleId查除了当前的记录 (vin)
	  @return
	 */
	public boolean validateVehicleByVin(Long vehicleId, String vin) {
		BaseExample example = vehicleManager.getExample();
		Criteria c = example.or();
		if (vehicleId != null) {
			c.andPropertyNotEqualTo(vehicleId, "id");
		}
		c.andPropertyEqualTo(vin, "vin");
		int count = vehicleManager.countByExample(example);
		return count > 0 ? true : false;
	}

	/**
	 * 根据vehicleId查除了当前的记录(ein)
	 * 
	 * @return
	 */
	public boolean validateVehicleByEin(Long vehicleId, String ein) {
		BaseExample example = vehicleManager.getExample();
		Criteria c = example.or();
		if (vehicleId != null) {
			c.andPropertyNotEqualTo(vehicleId, "id");
		}
		c.andPropertyEqualTo(ein, "ein");
		int count = vehicleManager.countByExample(example);
		return count > 0 ? true : false;
	}
    /*
    更新
     */
	public void update(Vehicle update) throws CarsmartException {
		vehicleManager.update(update);

	}
     /*
     增加
      */
	public void insert(Vehicle insert) throws CarsmartException {
		vehicleManager.insert(insert);

	}
     /*
     按BaseExample封装list
      */
	public List<Vehicle> get(List<Long> vidList) {
		BaseExample example = vehicleManager.getExample();
		example.or().andPropertyIn(vidList, "id");
		return vehicleManager.listByBaseExample(example);

	}
	/*
	删除
	 */
	public void delete(Long vehicleId) throws CarsmartException {
		vehicleManager.deleteByPrimaryKey(vehicleId);
	}
    /*
    验证车辆序列号
     */
	public boolean validateVehicleByVehicleSerial(Long vehicleId, String vehicleSerial) {
		BaseExample example = vehicleManager.getExample();
		Criteria c = example.or();
		if (vehicleId != null) {
			c.andPropertyNotEqualTo(vehicleId, "id");
		}
		c.andPropertyEqualTo(vehicleSerial, "serial");
		int count = vehicleManager.countByExample(example);
		return count > 0 ? true : false;
	}
	/*
	按序列号获取车辆信息
	 */
	public Vehicle getBySerial(String serial) {
		Vehicle query = new Vehicle();
		query.setSerial(serial);
		List<Vehicle> list = vehicleManager.listByExample(query);
		if(list == null || list.isEmpty()){
			return null;
		}
		return list.get(0);

	}
}
